package com.jzo2o.customer.service.impl;


import com.jzo2o.common.model.Result;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.IBankAccountMapper;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IBankAccountServiceImpl implements IBankAccountService {
    @Resource
    private IBankAccountMapper bankAccountMapper;

    @Override
    public Result<?> bankAccount(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {


        Long id = UserContext.getCurrentUser().getId();
        bankAccountUpsertReqDTO.setId(id);

        Integer install = bankAccountMapper.install(bankAccountUpsertReqDTO);

        if (install > 0) {
            return Result.error("该银行卡已被注册");
        }
        return Result.ok();
    }

    @Override
    public BankAccountUpsertReqDTO currentUserBankAccount() {
        Long id = UserContext.getCurrentUser().getId();
        BankAccountUpsertReqDTO bankAccountUpsertReqDTO = bankAccountMapper.selectByUserId(id);
        if (bankAccountUpsertReqDTO == null) {
            throw new RuntimeException("当前用户没有银行卡信息");
        }

        return bankAccountUpsertReqDTO;
    }
}
